//
//  WmcAppUpdateAlert.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/5/9.
//

import UIKit

// MARK: - [WmcAppUpdateAlert]

class WmcAppUpdateAlert: WmcBaseView {
    lazy var contentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.layer.cornerRadius = 8
        view.layer.masksToBounds = true
        return view
    }()

    lazy var rocketImageView: UIImageView = {
        let imageView = UIImageView(image: "app_update_rocket".wmc_image)
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()

    lazy var textImageView: UIImageView = {
        let imageView = UIImageView(image: "app_update_text".wmc_image)
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()

    lazy var closeBtn: UIButton = {
        let btn = UIButton()
        btn.setImage("close".wmc_image, for: .normal)
        return btn
    }()

    lazy var backgroundView: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_grayH19_F5F5F5
        view.layer.cornerRadius = 8
        view.layer.masksToBounds = true
        return view
    }()

    lazy var titleLabel: UILabel = {
        let label = UILabel.YP.Create(font: 18.yp.uiFont(weight: .bold),
                                      color: UIColor.wmc_grayH2_323233,
                                      alignment: .center)
        return label
    }()

    lazy var contentTextView: UITextView = {
        let textView = UITextView()
        textView.backgroundColor = .clear
        textView.isEditable = false
        textView.font = 14.wmc_font
        textView.textColor = .wmc_grayH5_606066
        return textView
    }()

    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_grayH15_E6E6E6
        return view
    }()

    lazy var updateBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("立即更新", for: .normal)
        btn.setTitleColor(.wmc_mainTheme, for: .normal)
        btn.titleLabel?.font = 16.yp.uiFont(weight: .bold)
        return btn
    }()

    /// 更新模型
    var model: WmcVersionInfoModel?

    override func makeUI() {
        backgroundColor = .clear

        let rocketTop = 128.wmc_autoHor * 0.578125

        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(rocketTop)
        }

        addSubview(rocketImageView)
        rocketImageView.snp.makeConstraints { make in
            make.centerX.top.equalToSuperview()
            make.width.equalTo(128.wmc_autoHor)
            make.height.equalTo(rocketImageView.snp.width).multipliedBy(1.171875)
        }

        contentView.addSubview(textImageView)
        textImageView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(rocketImageView.snp.bottom)
            make.height.equalTo(25)
        }

        contentView.addSubview(closeBtn)
        closeBtn.snp.makeConstraints { make in
            make.top.equalTo(12)
            make.right.equalTo(-12)
            make.size.equalTo(24)
        }

        contentView.addSubview(backgroundView)
        backgroundView.snp.makeConstraints { make in
            make.top.equalTo(textImageView.snp.bottom).offset(17)
            make.left.equalTo(Wmc_standardEdge.left)
            make.right.equalTo(-Wmc_standardEdge.right)
            make.bottom.equalTo(-72)
        }

        backgroundView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(Wmc_standardEdge.top)
        }

        backgroundView.addSubview(contentTextView)
        contentTextView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
            make.left.equalTo(Wmc_standardEdge.left)
            make.right.equalTo(-Wmc_standardEdge.right)
            make.bottom.equalToSuperview()
        }

        contentView.addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
            make.top.equalTo(backgroundView.snp.bottom).offset(24)
        }

        contentView.addSubview(updateBtn)
        updateBtn.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(lineView.snp.bottom)
        }
    }

    override func bindViewAction() {
        closeBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.dismiss()
        }

        updateBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            if !(weakSelf.model?.force ?? false) {
                weakSelf.dismiss()
            }
            if let url = URL(string: weakSelf.model?.path ?? "") {
                UIApplication.shared.openURL(url)
            }
        }
    }

    func configModel(_ model: WmcVersionInfoModel) {
        self.model = model
        titleLabel.text = "V\(model.versionNum) 更新"
        contentTextView.text = model.versionExplain
        closeBtn.isHidden = model.force
    }
}

// MARK: - WmcAlertViewProtocol

extension WmcAppUpdateAlert: WmcAlertViewProtocol {
    /// 显示
    func show() {
        var config: WmcPresentVC.Config =
            .basicConfig(view: self, style: .alert(), layoutBlock: { make in
                make.center.equalToSuperview()
                make.width.equalToSuperview().multipliedBy(0.85333)
                make.height.equalTo(440)
            })
        config.isTouchMaskDismiss = !(model?.force ?? true)
        show(config: config)
    }
}
